<?php
declare (strict_types = 1);

namespace app\controller;
// 引入校验
use app\validate\Menu as MenuValidate;
use think\exception\ValidateException;

use think\Request;

// model类
use app\model\Menu as MenuModel;

use think\facade\Db;

use app\service\Menu as MenuService;

class Menu extends Base
{
    /**
     * 接口基础数据
     *
     * @param  \think\Request  $request
     * @return json
     */
    public function index(Request $request)
    {
        $data = MenuModel::select()->toArray();
        
        $MenuService = new MenuService();
        $list = $MenuService->getMenuList();
        
        $class_type = array(0=>'一级目录',1=>'二级控制',2=>'三级操作');
        $menu = Db::table('admin_permission')->field('id,permission_name as name,permission_link as link')->where('class_type', 0)->select();
        $control = Db::table('admin_permission')->field('id,permission_name as name,permission_link as link,level_id')->where('class_type', 1)->select();
        return $this->createMsg(['list'=>$list,'type'=>$class_type,'menu'=>$menu,'control'=>$control],'查询成功!',200);
    }
    
    /**
     * 新增接口
     *
     * @param  \think\Request  $request
     * @return json
     */
    public function add(Request $request)
    {
        $params = request()->rsa_data;
        try {
            // 校验是否符合新增规则
            validate(MenuValidate::class)->scene('add')->check($params);
            if ($params['id'] === 0) {
                $newUser = MenuModel::create([
                    // 接口名
                    'permission_name'   =>  $params['name'],
                    // 接口权限值
                    'permission_value'  =>  $params['value'],
                    // 接口路径
                    'permission_link'   =>  $params['link'],
                    // 接口等级
                    'class_type'        =>  !empty($params['class_type']) ? $params['class_type'] : 0,
                    // 父ID
                    'level_id'          =>  !empty($params['level_id']) ? $params['level_id'] : 0,
                ]);
                return $this->createMsg($params,'添加成功!',200);
            }else{
                return $this->createMsg($params,'意外状态,请刷新重试!',400);
            }
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $this->createErrMsg([], $e->getError(), 400);
        }
    }
    
    /**
     * 修改接口
     *
     * @param  \think\Request  $request
     * @return json
     */
    public function edit(Request $request)
    {
        $params = request()->rsa_data;
        try {
            // 校验是否符合新增规则
            validate(MenuValidate::class)->scene('add')->check($params);
            if ($params['id'] !== 0) {
                $data = MenuModel::where('id', $params['id'])->find();
                if($data){
                    $data->permission_name  = $params['name'];
                    $data->permission_value = $params['value'];
                    $data->permission_link  = $params['link'];
                    $data->class_type       = $params['class_type'];
                    $data->level_id         = $params['level_id'];
                    if($data->save()){
                        return $this->createMsg($params,'修改成功!',200);
                    }else{ 
                        return $this->createMsg([], '编辑用户失败!', 400);
                    }
                }else{
                    return $this->createMsg($params,'找不到这个接口数据!',400);
                }
            }else{
                return $this->createMsg($params,'意外状态,请刷新重试!',400);
            }
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $this->createErrMsg([], $e->getError(), 400);
        }
    }
}